using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Medianamik.Core.DAL.SubSonicDAL
{
	/// <summary>
	/// Strongly-typed collection for the DataType class.
	/// </summary>
    [Serializable]
	public partial class DataTypeCollection : ActiveList<DataType, DataTypeCollection>
	{	   
		public DataTypeCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>DataTypeCollection</returns>
		public DataTypeCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                DataType o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the medianamik_DataType table.
	/// </summary>
	[Serializable]
	public partial class DataType : ActiveRecord<DataType>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public DataType()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public DataType(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public DataType(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public DataType(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("medianamik_DataType", TableType.Table, DataService.GetInstance("MedianamikProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarDataTypeID = new TableSchema.TableColumn(schema);
				colvarDataTypeID.ColumnName = "DataTypeID";
				colvarDataTypeID.DataType = DbType.Guid;
				colvarDataTypeID.MaxLength = 0;
				colvarDataTypeID.AutoIncrement = false;
				colvarDataTypeID.IsNullable = false;
				colvarDataTypeID.IsPrimaryKey = true;
				colvarDataTypeID.IsForeignKey = false;
				colvarDataTypeID.IsReadOnly = false;
				colvarDataTypeID.DefaultSetting = @"";
				colvarDataTypeID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDataTypeID);
				
				TableSchema.TableColumn colvarDataTypeSQLName = new TableSchema.TableColumn(schema);
				colvarDataTypeSQLName.ColumnName = "DataTypeSQLName";
				colvarDataTypeSQLName.DataType = DbType.AnsiString;
				colvarDataTypeSQLName.MaxLength = 50;
				colvarDataTypeSQLName.AutoIncrement = false;
				colvarDataTypeSQLName.IsNullable = false;
				colvarDataTypeSQLName.IsPrimaryKey = false;
				colvarDataTypeSQLName.IsForeignKey = false;
				colvarDataTypeSQLName.IsReadOnly = false;
				colvarDataTypeSQLName.DefaultSetting = @"";
				colvarDataTypeSQLName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDataTypeSQLName);
				
				TableSchema.TableColumn colvarMustSpecifyLength = new TableSchema.TableColumn(schema);
				colvarMustSpecifyLength.ColumnName = "MustSpecifyLength";
				colvarMustSpecifyLength.DataType = DbType.Boolean;
				colvarMustSpecifyLength.MaxLength = 0;
				colvarMustSpecifyLength.AutoIncrement = false;
				colvarMustSpecifyLength.IsNullable = false;
				colvarMustSpecifyLength.IsPrimaryKey = false;
				colvarMustSpecifyLength.IsForeignKey = false;
				colvarMustSpecifyLength.IsReadOnly = false;
				colvarMustSpecifyLength.DefaultSetting = @"";
				colvarMustSpecifyLength.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMustSpecifyLength);
				
				TableSchema.TableColumn colvarHidden = new TableSchema.TableColumn(schema);
				colvarHidden.ColumnName = "Hidden";
				colvarHidden.DataType = DbType.Boolean;
				colvarHidden.MaxLength = 0;
				colvarHidden.AutoIncrement = false;
				colvarHidden.IsNullable = false;
				colvarHidden.IsPrimaryKey = false;
				colvarHidden.IsForeignKey = false;
				colvarHidden.IsReadOnly = false;
				colvarHidden.DefaultSetting = @"";
				colvarHidden.ForeignKeyTableName = "";
				schema.Columns.Add(colvarHidden);
				
				TableSchema.TableColumn colvarIndexable = new TableSchema.TableColumn(schema);
				colvarIndexable.ColumnName = "Indexable";
				colvarIndexable.DataType = DbType.Boolean;
				colvarIndexable.MaxLength = 0;
				colvarIndexable.AutoIncrement = false;
				colvarIndexable.IsNullable = false;
				colvarIndexable.IsPrimaryKey = false;
				colvarIndexable.IsForeignKey = false;
				colvarIndexable.IsReadOnly = false;
				colvarIndexable.DefaultSetting = @"";
				colvarIndexable.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIndexable);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MedianamikProvider"].AddSchema("medianamik_DataType",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("DataTypeID")]
		[Bindable(true)]
		public Guid DataTypeID 
		{
			get { return GetColumnValue<Guid>(Columns.DataTypeID); }
			set { SetColumnValue(Columns.DataTypeID, value); }
		}
		  
		[XmlAttribute("DataTypeSQLName")]
		[Bindable(true)]
		public string DataTypeSQLName 
		{
			get { return GetColumnValue<string>(Columns.DataTypeSQLName); }
			set { SetColumnValue(Columns.DataTypeSQLName, value); }
		}
		  
		[XmlAttribute("MustSpecifyLength")]
		[Bindable(true)]
		public bool MustSpecifyLength 
		{
			get { return GetColumnValue<bool>(Columns.MustSpecifyLength); }
			set { SetColumnValue(Columns.MustSpecifyLength, value); }
		}
		  
		[XmlAttribute("Hidden")]
		[Bindable(true)]
		public bool Hidden 
		{
			get { return GetColumnValue<bool>(Columns.Hidden); }
			set { SetColumnValue(Columns.Hidden, value); }
		}
		  
		[XmlAttribute("Indexable")]
		[Bindable(true)]
		public bool Indexable 
		{
			get { return GetColumnValue<bool>(Columns.Indexable); }
			set { SetColumnValue(Columns.Indexable, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		private Medianamik.Core.DAL.SubSonicDAL.InterfacePropertyCollection colInterfacePropertyRecords;
		public Medianamik.Core.DAL.SubSonicDAL.InterfacePropertyCollection InterfacePropertyRecords()
		{
			if(colInterfacePropertyRecords == null)
			{
				colInterfacePropertyRecords = new Medianamik.Core.DAL.SubSonicDAL.InterfacePropertyCollection().Where(InterfaceProperty.Columns.DataTypeId, DataTypeID).Load();
				colInterfacePropertyRecords.ListChanged += new ListChangedEventHandler(colInterfacePropertyRecords_ListChanged);
			}
			return colInterfacePropertyRecords;
		}
				
		void colInterfacePropertyRecords_ListChanged(object sender, ListChangedEventArgs e)
		{
            if (e.ListChangedType == ListChangedType.ItemAdded)
            {
		        // Set foreign key value
		        colInterfacePropertyRecords[e.NewIndex].DataTypeId = DataTypeID;
				colInterfacePropertyRecords.ListChanged += new ListChangedEventHandler(colInterfacePropertyRecords_ListChanged);
            }
		}
		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varDataTypeID,string varDataTypeSQLName,bool varMustSpecifyLength,bool varHidden,bool varIndexable)
		{
			DataType item = new DataType();
			
			item.DataTypeID = varDataTypeID;
			
			item.DataTypeSQLName = varDataTypeSQLName;
			
			item.MustSpecifyLength = varMustSpecifyLength;
			
			item.Hidden = varHidden;
			
			item.Indexable = varIndexable;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varDataTypeID,string varDataTypeSQLName,bool varMustSpecifyLength,bool varHidden,bool varIndexable)
		{
			DataType item = new DataType();
			
				item.DataTypeID = varDataTypeID;
			
				item.DataTypeSQLName = varDataTypeSQLName;
			
				item.MustSpecifyLength = varMustSpecifyLength;
			
				item.Hidden = varHidden;
			
				item.Indexable = varIndexable;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn DataTypeIDColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn DataTypeSQLNameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn MustSpecifyLengthColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn HiddenColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn IndexableColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string DataTypeID = @"DataTypeID";
			 public static string DataTypeSQLName = @"DataTypeSQLName";
			 public static string MustSpecifyLength = @"MustSpecifyLength";
			 public static string Hidden = @"Hidden";
			 public static string Indexable = @"Indexable";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
                if (colInterfacePropertyRecords != null)
                {
                    foreach (Medianamik.Core.DAL.SubSonicDAL.InterfaceProperty item in colInterfacePropertyRecords)
                    {
                        if (item.DataTypeId != DataTypeID)
                        {
                            item.DataTypeId = DataTypeID;
                        }
                    }
               }
		}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
                if (colInterfacePropertyRecords != null)
                {
                    colInterfacePropertyRecords.SaveAll();
               }
		}
        #endregion
	}
}
